package com.ibeeking.found.common.utils;

import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.enums.LoginRoleEnum;
import com.ibeeking.nematos.constants.enums.LogConstant;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.StringUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.logging.log4j.ThreadContext;

/**
 * 用户工具类
 */
public class UserUtils {

    /**
     * 获取当前用户id
     *
     * @return
     */
    public static Long getUserId() {
        String userId = getStringUserId();
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        return Long.valueOf(userId);
    }

    /**
     * 获取当前用户id
     *
     * @return
     */
    public static String getStringUserId() {
        return ThreadContext.get(LogConstant.USER_ID);
    }

    /**
     * 获取当前用户id
     *
     * @return
     */
    public static Boolean isDemoUser() {
        String amDemouser = ThreadContext.get(HeaderNounConstant.AM_DEMO_USER);
        if (StringUtils.isBlank(amDemouser)) return false;
        Boolean demouser = BooleanUtils.toBoolean(amDemouser);
        return demouser;
    }

    /**
     * 获取当前用户所属商户id
     *
     * @return
     */
    public static Long getShopId() {
        String loginSystem = ThreadContext.get(HeaderNounConstant.LOGIN_SYSTEM);
        String shopId = ThreadContext.get(LogConstant.SHOP_ID);
        if (LoginRoleEnum.SHOP.name().equals(loginSystem)) {
            if (StringUtils.isBlank(shopId)) {
                throw BusinessException.create("无法获取当前登录用户的商户ID");
            }
            return Long.valueOf(shopId);
        } else if (LoginRoleEnum.TENANT.name().equals(loginSystem)) {
            return GlobalConstant.SHOP_ID_ZERO;
        }
        throw BusinessException.create("无法获取当前登录用户的商户ID");
    }

    public static Long getShopId(CurrentUser currentUser) {
        String loginSystem = currentUser.getLoginSystem();
        if (LoginRoleEnum.SHOP.name().equals(loginSystem)) {
            Long shopId = currentUser.getCurrentShop().getShopId();
            if (null == shopId) {
                throw BusinessException.create("无法获取当前登录用户的商户ID");
            }
            return shopId;
        } else if (LoginRoleEnum.TENANT.name().equals(loginSystem)) {
            return GlobalConstant.SHOP_ID_ZERO;
        }
        throw BusinessException.create("无法获取当前登录用户的商户ID");
    }

    /**
     * 获取当前用户所属商户id 如果为空抛出异常!
     *
     * @return
     */
    @Deprecated
    public static Long getShopIdException() {
        return getShopId();
    }

    public static String getTenantId() {
        String tenantId = ThreadContext.get(HeaderNounConstant.TENANT_ID);
        DataUtils.trueThrowException(StringUtils.isBlank(tenantId), "无法获取当前租户");
        return tenantId;
    }

    public static Long getTenantID() {
        String tenantId = ThreadContext.get(HeaderNounConstant.TENANT_ID);
        DataUtils.trueThrowException(StringUtils.isBlank(tenantId), "无法获取当前租户");
        return Long.valueOf(tenantId);
    }

    public static Integer getTenantid() {
        String tenantId = ThreadContext.get(HeaderNounConstant.TENANT_ID);
        DataUtils.trueThrowException(StringUtils.isBlank(tenantId), "无法获取当前租户");
        return Integer.valueOf(tenantId);
    }

    public static Integer getTenantId(CurrentUser currentUser) {
        Integer tenantId = currentUser.getTenantId();
        DataUtils.trueThrowException(null == tenantId, "无法获取当前租户");
        return tenantId;
    }

    public static String getTenantIndustry() {
        String tenantIndustry = ThreadContext.get(HeaderNounConstant.INDUSTRY);
        DataUtils.trueThrowException(StringUtils.isBlank(tenantIndustry), "无法获取当前租户所属行业");
        return tenantIndustry;
    }

}
